package com.zcb.myrxjavathreecache.tools;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.view.WindowManager;

import java.util.ArrayList;

public class AppContext extends Application {
    private Context context;
    public final static boolean IsDeBug = true;
    String appDir;
    String imageCacheDir;
    private static AppContext instance;
    private static ArrayList<Activity> activitys = null;
    private WindowManager wm;
    private double lat = 0;
    public static String IMAGES_DIR = Environment.getExternalStorageDirectory()
            .getPath() + "/WZLY/saveImages/";
    private double lng = 0;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        instance = this;
        wm = (WindowManager) instance.getSystemService(Context.WINDOW_SERVICE);
        context = getApplicationContext();
        appDir = Environment.getExternalStorageDirectory().getPath() + "/Wzly/";
        imageCacheDir = "Wzly/images";

    }



    public static AppContext getInstance() {
        return instance;
    }

    public static void LogInfo(String Tag, String s) {
        if (IsDeBug) {
            Log.e(Tag, s);
        }
    }

    /**
     * 添加Activity到容器中
     *
     * @param activity
     */
    public static void addActivity(Activity activity) {
        if (activitys != null && activitys.size() > 0) {
            if (!activitys.contains(activity)) {
                activitys.add(activity);
            }
        } else {
            activitys.add(activity);
        }
    }

    /**
     * @param activity
     */
    public static void DeleActivity(String activity) {
        for (int i = 0; i < activitys.size(); i++) {
            LogInfo("activitys.get(i).getClass().getName()", activitys.get(i)
                    .getClass().getName());
            if (activitys.get(i).getClass().getName().equals(activity)) {
                LogInfo("activitys", activitys.get(i).getClass().getName());
                activitys.get(i).finish();
                activitys.remove(i);
            }

        }

    }

    /**
     * 遍历所有Activity并finish
     */
    public static void exit() {
        if (activitys != null && activitys.size() > 0) {
            for (Activity activity : activitys) {
                activity.finish();
            }
        }
    }

    // 获取屏幕宽度
    public int getWindowWith() {

        return wm.getDefaultDisplay().getWidth();
    }

    /**
     * 获取屏幕高度
     *
     * @return
     */
    public int getWindowHeigth() {

        return wm.getDefaultDisplay().getHeight();
    }


    public double Getlat() {
        return lat;
    }

    public double Getlng() {
        return lng;
    }


    private void setLat(double lat) {
        this.lat = lat;
    }

    private void setLng(double lng) {
        this.lng = lng;
    }


}
